//
//  UIDevice+OneHardware.m
//  CreditManger
//
//  Created by chaiyanan on 15/11/5.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "UIDevice+OneHardware.h"
#include <sys/sysctl.h>

@implementation UIDevice (OneHardware)

- (NSString *)getSystemInfoByName:(char *)typeSpecifier {
    size_t size;
    sysctlbyname(typeSpecifier, NULL, &size, NULL, 0);
    
    char *answer = malloc(size);
    sysctlbyname(typeSpecifier, answer, &size, NULL, 0);
    
    NSString *results = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];
    
    free(answer);
    return results;
}

/**
 *  硬件平台
 *
 *  @return String
 */
+ (NSString *)platform{
    return [[UIDevice alloc] getSystemInfoByName:"hw.machine"];
}

/**
 *  平台类型
 *
 *  @return platform Type
 */
+ (ONGPlatformType)platformType {
    NSString *platform = [self platform];
    
    if ([platform isEqualToString:@"iPhone1,1"])    return ONGPlatformTypeiPhone2G;
    if ([platform isEqualToString:@"iPhone1,2"])    return ONGPlatformTypeiPhone3G;
    if ([platform isEqualToString:@"iPhone2,1"])    return ONGPlatformTypeiPhone3GS;
    if ([platform isEqualToString:@"iPhone3,1"])    return ONGPlatformTypeiPhone4;
    if ([platform isEqualToString:@"iPhone3,2"])    return ONGPlatformTypeiPhone4;
    if ([platform isEqualToString:@"iPhone3,3"])    return ONGPlatformTypeiPhone4;
    if ([platform isEqualToString:@"iPhone4,1"])    return ONGPlatformTypeiPhone4s;
    if ([platform isEqualToString:@"iPhone5,1"])    return ONGPlatformTypeiPhone5;
    if ([platform isEqualToString:@"iPhone5,2"])    return ONGPlatformTypeiPhone5;
    if ([platform isEqualToString:@"iPhone5,3"])    return ONGPlatformTypeiPhone5c;
    if ([platform isEqualToString:@"iPhone5,4"])    return ONGPlatformTypeiPhone5c;
    if ([platform isEqualToString:@"iPhone6,1"])    return ONGPlatformTypeiPhone5s;
    if ([platform isEqualToString:@"iPhone6,2"])    return ONGPlatformTypeiPhone5s;
    if ([platform isEqualToString:@"iPhone7,1"])    return ONGPlatformTypeiPhone6Plus;
    if ([platform isEqualToString:@"iPhone7,2"])    return ONGPlatformTypeiPhone6;
    if ([platform isEqualToString:@"iPhone8,1"])    return ONGPlatformTypeiPhone6s;
    if ([platform isEqualToString:@"iPhone8,2"])    return ONGPlatformTypeiPhone6sPlus;
    if ([platform isEqualToString:@"iPod1,1"])      return ONGPlatformTypeiPodTouch1G;
    if ([platform isEqualToString:@"iPod2,1"])      return ONGPlatformTypeiPodTouch2G;
    if ([platform isEqualToString:@"iPod3,1"])      return ONGPlatformTypeiPodTouch3G;
    if ([platform isEqualToString:@"iPod4,1"])      return ONGPlatformTypeiPodTouch4G;
    if ([platform isEqualToString:@"iPod5,1"])      return ONGPlatformTypeiPodTouch5G;
    if ([platform isEqualToString:@"iPad1,1"])      return ONGPlatformTypeiPad1G;
    if ([platform isEqualToString:@"iPad2,1"])      return ONGPlatformTypeiPad2;
    if ([platform isEqualToString:@"iPad2,2"])      return ONGPlatformTypeiPad2;
    if ([platform isEqualToString:@"iPad2,3"])      return ONGPlatformTypeiPad2;
    if ([platform isEqualToString:@"iPad2,4"])      return ONGPlatformTypeiPad2;
    if ([platform isEqualToString:@"iPad2,5"])      return ONGPlatformTypeiPadMini1G;
    if ([platform isEqualToString:@"iPad2,6"])      return ONGPlatformTypeiPadMini1G;
    if ([platform isEqualToString:@"iPad2,7"])      return ONGPlatformTypeiPadMini1G;
    if ([platform isEqualToString:@"iPad3,1"])      return ONGPlatformTypeiPad3;
    if ([platform isEqualToString:@"iPad3,2"])      return ONGPlatformTypeiPad3;
    if ([platform isEqualToString:@"iPad3,3"])      return ONGPlatformTypeiPad3;
    if ([platform isEqualToString:@"iPad3,4"])      return ONGPlatformTypeiPad4;
    if ([platform isEqualToString:@"iPad3,5"])      return ONGPlatformTypeiPad4;
    if ([platform isEqualToString:@"iPad3,6"])      return ONGPlatformTypeiPad4;
    if ([platform isEqualToString:@"iPad4,1"])      return ONGPlatformTypeiPadAir;
    if ([platform isEqualToString:@"iPad4,2"])      return ONGPlatformTypeiPadAir;
    if ([platform isEqualToString:@"iPad4,3"])      return ONGPlatformTypeiPadAir;
    if ([platform isEqualToString:@"iPad4,4"])      return ONGPlatformTypeiPadMini2;
    if ([platform isEqualToString:@"iPad4,5"])      return ONGPlatformTypeiPadMini2;
    if ([platform isEqualToString:@"iPad4,6"])      return ONGPlatformTypeiPadMini2;
    if ([platform isEqualToString:@"iPad4,7"])      return ONGPlatformTypeiPadMini3;
    if ([platform isEqualToString:@"iPad4,8"])      return ONGPlatformTypeiPadMini3;
    if ([platform isEqualToString:@"iPad4,9"])      return ONGPlatformTypeiPadMini3;
    if ([platform isEqualToString:@"iPad5,3"])      return ONGPlatformTypeiPadAir2;
    if ([platform isEqualToString:@"iPad5,4"])      return ONGPlatformTypeiPadAir2;
    if ([platform isEqualToString:@"i386"])         return ONGPlatformTypeSimulator;
    if ([platform isEqualToString:@"x86_64"])       return ONGPlatformTypeSimulator;
    if ([platform hasPrefix:@"iPhone"])             return ONGPlatformTypeUnknowniPhone;
    if ([platform hasPrefix:@"iPad"])               return ONGPlatformTypeUnknowniPad;
    if ([platform hasPrefix:@"iPod"])               return ONGPlatformTypeUnknowniPod;
    if ([platform isEqualToString:@"AppleTV2,1"])   return ONGPlatformTypeAppleTV2G;
    if ([platform isEqualToString:@"AppleTV3,1"])   return ONGPlatformTypeAppleTV3G;
    if ([platform isEqualToString:@"AppleTV3,2"])   return ONGPlatformTypeAppleTV3G;
    
    return ONGPlatformTypeUnknown;
}

/**
 *  平台字符串
 *
 *  @return platform String
 */
+ (NSString *)platformString {
    switch ([UIDevice platformType]) {
        case ONGPlatformTypeUnknown:             return @"Unknow";
        case ONGPlatformTypeiPad1G:              return @"iPad 1G";
        case ONGPlatformTypeiPad2:               return @"iPad 2";
        case ONGPlatformTypeiPad3:               return @"iPad 3";
        case ONGPlatformTypeiPad4:               return @"iPad 4";
        case ONGPlatformTypeiPadAir:             return @"iPad Air";
        case ONGPlatformTypeiPadAir2:            return @"iPad Air 2";
        case ONGPlatformTypeiPadMini1G:          return @"iPad Mini 1G";
        case ONGPlatformTypeiPadMini2:           return @"iPad Mini 2";
        case ONGPlatformTypeiPadMini3:           return @"iPad Mini 3";
        case ONGPlatformTypeiPhone2G:            return @"iPhone 2G";
        case ONGPlatformTypeiPhone3G:            return @"iPhone 3G";
        case ONGPlatformTypeiPhone3GS:           return @"iPhone 3GS";
        case ONGPlatformTypeiPhone4:             return @"iPhone 4";
        case ONGPlatformTypeiPhone4s:            return @"iPhone 4s";
        case ONGPlatformTypeiPhone5:             return @"iPhone 5";
        case ONGPlatformTypeiPhone5c:            return @"iPhone 5c";
        case ONGPlatformTypeiPhone5s:            return @"iPhone 5s";
        case ONGPlatformTypeiPhone6:             return @"iPhone 6";
        case ONGPlatformTypeiPhone6Plus:         return @"iPhone 6 Plus";
        case ONGPlatformTypeiPhone6s:             return @"iPhone 6s";
        case ONGPlatformTypeiPhone6sPlus:         return @"iPhone 6s Plus";
        case ONGPlatformTypeiPodTouch1G:         return @"iPod Touch 1G";
        case ONGPlatformTypeiPodTouch2G:         return @"iPod Touch 2G";
        case ONGPlatformTypeiPodTouch3G:         return @"iPod Touch 3G";
        case ONGPlatformTypeiPodTouch4G:         return @"iPod Touch 4G";
        case ONGPlatformTypeiPodTouch5G:         return @"iPod Touch 5G";
        case ONGPlatformTypeAppleTV2G:           return @"Apple TV 2G";
        case ONGPlatformTypeAppleTV3G:           return @"Apple TV 3G";
        case ONGPlatformTypeSimulator:           return @"Simulator";
        case ONGPlatformTypeUnknowniPhone:       return @"Unknown iPhone";
        case ONGPlatformTypeUnknowniPod:         return @"Unknown iPod";
        case ONGPlatformTypeUnknowniPad:         return @"Unknown iPad";
    }
//    return nil;
    return @"";
}

/**
 *  设备分辨率
 *
 *  @return platform String
 */
+ (NSString *)platformResolution
{
    CGRect rect_screen = [[UIScreen mainScreen]bounds];
    CGSize size_screen = rect_screen.size;
    
    CGFloat scale_screen = [UIScreen mainScreen].scale;
    return [NSString stringWithFormat:@"%dx%d",(int)(scale_screen*size_screen.width),(int)(scale_screen*size_screen.height)];
}

/**
 *  设备UUID
 *
 *  @return uniqueString String
 */
+ (NSString *)uniqueString
{
    CFUUIDRef	uuidObj = CFUUIDCreate(nil);
    NSString	*uuidString = (__bridge_transfer NSString*)CFUUIDCreateString(nil, uuidObj);
    CFRelease(uuidObj);
    return uuidString;
}


@end
